package com.mifengkong.frwidget.banner;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.mifengkong.frwidget.R;

/**
 * Created by jiangyongxing on 2017/8/22.
 * 描述：
 */

public class IndicatorView extends AbsIndicatorView {


    /**
     * anchor container
     */
    private LinearLayout indicateLayout;

    public IndicatorView(Context context) {
        super(context);
    }

    public IndicatorView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public IndicatorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void findView() {
        this.indicateLayout = (LinearLayout) findViewById(R.id.image_indicater_layout);
    }

    @Override
    protected int layoutRes() {
        return R.layout.viewpager_indicator_layout;
    }

    @Override
    public void show() {
        indicateLayout.removeAllViews();
        for (int index = 0; index < this.totelCount; index++) {

            final View indicater = new ImageView(getContext());

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.leftMargin = 10;
            indicater.setLayoutParams(params);
            this.indicateLayout.addView(indicater, index);
        }
        this.refreshHandler.sendEmptyMessage(currentIndex);
    }

    @Override
    protected void refreshIndicateView() {
        {
            for (int index = 0; index < totelCount; index++) {
                final ImageView imageView = (ImageView) this.indicateLayout.getChildAt(index);
                if (this.currentIndex == index) {
                    imageView.setBackgroundResource(R.drawable.dot_focus);
                } else {
                    imageView.setBackgroundResource(R.drawable.dot_normal);
                }

            }

        }
    }
}
